const db = wx.cloud.database();
Page({
  data: {
    type:null,
    collectList:null
  },

  //清空收藏
  clearCollectDo: function () {
    let type = this.data.type;

    if ("article" == type) {
      wx.removeStorageSync('my_article');
    } else if ("error" == type) {
      wx.removeStorageSync('my_question');
    } else if ("word" == type) {
      wx.removeStorageSync('my_word');
    }
    this.setData({
      collectList: null
    });
  },

  clearCollect: function () {
    let that = this;
    wx.showModal({
      title: '温馨提示',
      content: '您确定清空收藏吗？清空后无法恢复。',
      confirmText: "确定",
      cancelText: "取消",
      success: function (res) {
        console.log(res);
        if (res.confirm) {
          that.clearCollectDo();
        } else {
          
        }
      }
    });
  },

  //查询试题详情
  showQuestionDetail: function (e) {
    let idx = e.currentTarget.dataset.idx;
    let question = this.data.collectList[idx];
    let data = JSON.stringify(question);

    wx.navigateTo({
      url: '/pages/tools/result/question-detail?data=' + data,
    })
  },

  //查询试题详情
  showWordDetail: function (e) {
    let idx = e.currentTarget.dataset.idx;
    let word = this.data.collectList[idx];
    let data = JSON.stringify(word);

    wx.navigateTo({
      url: '/pages/common/word?showCollect=0&word=' + word.word+'&data=' + data,
    })
  },

  onLoad: function (options) {
    let type = options.type;
    this.setData({
      type: type
    });
    let title = null;
    let collectList = null;

    if ("article" == type)
    {
      title = "我收藏的文章";
      collectList = wx.getStorageSync('my_article');

    } else if ("error" == type)
    {
      title = "我的错题本";
      collectList = wx.getStorageSync('my_question');
    } else if ("word" == type)
    {
      title = "我收藏的词汇";
      collectList = wx.getStorageSync('my_word');
    }

    //改变标题title
    wx.setNavigationBarTitle({
      title: title
    });

    this.setData({
      collectList: collectList
    });
    
  }
})